package com.shipwe.loans.ui.presenter

import com.shipwe.loans.configs.ShipweConfigs
import com.shipwe.loans.http.ApiService
import com.shipwe.loans.http.IHttpCallback
import com.shipwe.loans.pro.ShipwePresenter
import com.shipwe.loans.ui.view.IAppSplashView
import com.shipwe.loans.utils.helper.ShipweAppAnalysisHelper
import com.shipwe.loans.utils.shared.ShipweSharedHelper
import com.shipwe.loans.utils.shared.ShipweSharedKey
import com.shipwe.permission.Permission
import com.shipwe.permission.XXPermissions
import org.json.JSONObject
import java.util.TimeZone

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/28 13:45
 * @Annotation：贷款申请 Presenter 实现
 */
class AppSplashPresenter(private val iView: IAppSplashView) : ShipwePresenter(iView) {

    /**
     * 获取地区转发
     */
    fun loadAppCommonTimeZoneAction() {
        iView.showShipweLoading()
        val params = JSONObject()
        params.put("p", ShipweConfigs.SHIPWE_APPNAME)
        params.put("tz", TimeZone.getDefault().id)
        val httpUrl = ApiService.SHIPWE_HOST + ApiService.API_URL.SHIPWE_API_TIMEZONE_URL
        doHttpPostAction(httpUrl, params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.showSplashTimeZoneRequestData(data.optString("data"))
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 获取APP配置信息
     */
    fun loadAppCommonConfigsAction() {
        val params = JSONObject().apply {
            put("product",ShipweConfigs.SHIPWE_APPNAME)
            put("path",ApiService.API_PATH.SHIPWE_API_APP_INFO_PATH)
        }
        val httpUrl = ApiService.getHttpUrl()
        doHttpPostAction(httpUrl, params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.dismissShipweLoading()
                val obj = data.optJSONObject("obj")
                //保存所有数据
                if (obj != null) {
                    ShipweSharedHelper.putShipweSharedData(ShipweSharedKey.SHIPWE_COMMON,obj.toString())
                }

                // TODO: PhoneState为必须申请的权限
                if (!XXPermissions.isGranted(iView.getAct(), Permission.READ_PHONE_STATE)){
                    //进入权限页
                    iView.showSplashPermissionPage()
                } else {
                    //已登录 - 进入主页
                    if (ShipweSharedHelper.getShipweSharedBoolean(ShipweSharedKey.SHIPWE_LOGIN)){
                        //初始化analysis lib 赋值 用户id 和 token
                        ShipweAppAnalysisHelper.initAnalysisLibAtLoginSuccess()
                        iView.showSplashMainPage()
                    } else {//进入登录页
                        iView.showSplashLoginPage()
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.dismissShipweLoading()
            }
        })
    }
}